﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data;

namespace DAL
{
    public class LeaseModeService
    {
        public static LeaseMode AddLeaseMode(LeaseMode lMode)
        {
            string sql = string.Format("insert into LeaseMode(Id,Name) values({0},N'{1}');", lMode.Id, lMode.Name);
            DBHelper.GetScalar(sql);
            return GetLeaseModeById(lMode.Id);
        }

        public static LeaseMode GetLeaseModeById(int id)
        {
            string sql = string.Format("select * from LeaseMode where Id={0}", id);
            return GetLeaseModeBySql(sql);
        }

        public static List<LeaseMode> GetAllLeaseModes()
        {
            string sql = string.Format("select * from LeaseMode");
            return GetLeaseModesBySql(sql);
        }

        private static LeaseMode GetLeaseModeBySql(string sql)
        {
            List<LeaseMode> list = GetLeaseModesBySql(sql);
            if (list.Count() > 0)
                return list[0];
            return null;
        }

        private static List<LeaseMode> GetLeaseModesBySql(string sql)
        {
            List<LeaseMode> list = new List<LeaseMode>();
            DataTable dt = DBHelper.GetDataSet(sql);
            foreach (DataRow row in dt.Rows)
            {
                LeaseMode lMode = new LeaseMode();
                lMode.Id = (int)row["Id"];
                lMode.Name = (string)row["Name"];
                list.Add(lMode);
            }
            return list;
        }
    }
}
